<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minMutation = function (startGene, endGene, bank) {
      let bankSet = new Set(bank)
      if (!bankSet.has(endGene)) {
        return -1
      }

      let p = []
      p.push(startGene)
      let visited = new Set()
      visited.add(startGene)
      let step = 0
      let arr = ['A', 'C', 'G', 'T']

      while (p.length) {
        let sz = p.length
        for (let i = 0; i < sz; i++) {
          let cur = p.shift()
          if (cur === endGene) {
            return step
          }
          for (let j = 0; j < cur.length; j++) {
            let char = cur[j]
            for (let c = 0; c < arr.length; c++) {
              if (c === char) continue
              let newWord = ''
              if (j === cur.length - 1) {
                newWord = cur.slice(0, j) + arr[c]
              } else {
                newWord = cur.slice(0, j) + arr[c] + cur.slice(j + 1)
              }
              if (bankSet.has(newWord) && !visited.has(newWord)) {
                p.push(newWord)
                visited.add(newWord)
              }
            }

          }
        }

        step++
      }

      return -1
    };
    console.log(minMutation('AACCGGTT', 'AAACGGTA', ["AACCGGTA","AACCGCTA","AAACGGTA"]));
  </script>
</body>

</html>